package com.zdya.combatdocuments.util;
import cn.hutool.extra.servlet.ServletUtil;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

public class SystemUtils {

    /**
     * 判断操作系统是否是 Windows
     *
     * @return true：操作系统是 Windows
     *         false：其它操作系统
     */
    public static boolean isWindows() {
        String osName = getOsName();

        return osName != null && osName.startsWith("Windows");
    }

    /**
     * 判断操作系统是否是 MacOS
     *
     * @return true：操作系统是 MacOS
     *         false：其它操作系统
     */
    public static boolean isMacOs() {
        String osName = getOsName();

        return osName != null && osName.startsWith("Mac");
    }

    /**
     * 判断操作系统是否是 Linux
     *
     * @return true：操作系统是 Linux
     *         false：其它操作系统
     */
    public static boolean isLinux() {
        String osName = getOsName();

        return (osName != null && osName.startsWith("Linux")) || (!isWindows() && !isMacOs());
    }

    /**
     * 获取操作系统名称
     * @return os.name 属性值
     */
    public static String getOsName() {
        return System.getProperty("os.name");
    }

    public static void main(String[] args) {
        System.out.println(getOsName());
    }

    /**
     * @desc 获取发送请求的电脑的IP地址，并返回
     * @return
     */
    public static String getRequestIp() {
        // 获取请求属性
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        // 从请求属性中获取HttpServletRequest的信息
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        String clientIP = ServletUtil.getClientIP(request);
        return clientIP;
    }
}
